package bean.life.xml;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

/**
 * TODO  如果某个类实现了BeanPostProcessor接口，那么该类的生命周期会在
 *  registerBeanPostProcessors(beanFactory)时就执行，
 *  包括 创建实例，填充属性，执行setName，SetBeanFactory，init-method,afterProperties。
 *  因为需要先创建BeanPostProcessors对象，后期才能在实例化其他Bean的时候拦截
 */
@Component
public class XMLBeanPostProcessor implements BeanPostProcessor, InitializingBean {
	public XMLBeanPostProcessor() {
		System.out.println("XMLBeanFactoryPostProcessor 实例化");
	}

	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		if(beanName.equals("myConfig")){
			System.out.println("111");
		}
		System.out.println("5 postProcessBeforeInitialization, bean = " + beanName);
		return bean;
	}

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		System.out.println("9 postProcessAfterInitialization , bean = " + beanName);
		return bean;
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("XMLBeanPostProcessor ---> afterPropertiesSet");
	}
}
